package com.seal.yoyoshop.service.impl;

import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.dao.UserDao;
import com.seal.yoyoshop.dao.impl.UserDaoImpl;
import com.seal.yoyoshop.service.UserService;
import com.seal.yoyoshop.util.SafeUtil;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public User getUserByUsername(String username) {
        User user = userDao.selectUserByUsername(username);
        return user;
    }

    @Override
    public int newUser(User user) {
//        密码加密
        String password = user.getPassword();
        String pwd =  SafeUtil.encode(password);
        user.setPassword(pwd);

        int flag = userDao.insertUser(user);
        return flag;
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
//        明文密码加密
        password = SafeUtil.encode(password);

        User user = userDao.selectUserByUsernameAndPassword(username,password);
        return user;
    }

    @Override
    public int updateUserPassword(String username, String password) {
        //        密码加密
        String pwd =  SafeUtil.encode(password);
        int flag = userDao.updatePassword(username,pwd);
        return flag;
    }

    @Override
    public boolean updateUser(User user) {
        boolean flag = userDao.updateUser(user);
        return flag;
    }

    @Override
    public List<User> getUserList(int page, int size) {
        List<User> userList = userDao.selectUser(page,size);
        return userList;
    }

    @Override
    public long getUserCount() {

        long count = userDao.selectUserCount();
        return count;
    }

    @Override
    public User getUserById(int id) {
        User user = userDao.selectUserById(id);
        return user;
    }
}
